package lx.af.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import lx.af.R;
import lx.af.manager.ActivityManager;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * author: liuxu
 * date: 2015-02-06
 *
 * activity base
 */
public abstract class BaseActivity extends FragmentActivity {

    private static ActivityManager mActivityManager = ActivityManager.getInstance();

    protected String TAG = "liuxu";
    private View mContentView;
    private boolean mIsForeground = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        mActivityManager.onActivityCreate(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mActivityManager.onActivityResume(this);
        mIsForeground = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mActivityManager.onActivityPause(this);
        mIsForeground = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mActivityManager.onActivityDestory(this);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
    }

    @Override
    public View findViewById(int id) {
        View v = super.findViewById(id);
        if (v == null && mContentView != null)
            return mContentView.findViewById(id);
        return v;
    }

    @Override
    public void setContentView(int layoutResID) {
        if (this instanceof ActionBarImpl) {
            super.setContentView(R.layout.activity_base);
            LayoutInflater inflater = LayoutInflater.from(this);
            View contentView = inflater.inflate(layoutResID, null);
            initActionBar(contentView, null);
        } else {
            super.setContentView(layoutResID);
        }
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (this instanceof ActionBarImpl) {
            super.setContentView(R.layout.activity_base);
            initActionBar(view, params);
        } else {
            super.setContentView(view, params);
        }
    }

    @Override
    public void setContentView(View view) {
        if (this instanceof ActionBarImpl) {
            super.setContentView(R.layout.activity_base);
            initActionBar(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        } else {
            super.setContentView(view);
        }
    }

    /**
     * check if the activity is running in foreground.
     * AKA, if the activity is in life cycle between onResume() and onPause().
     * @return true if running in foreground.
     */
    public boolean isForeground() {
        return mIsForeground;
    }

    /**
     * get and convert view
     * @param id view id for findViewById() method
     * @param <T> subclass of View
     * @return the view
     */
    @SuppressWarnings("unchecked")
    public  <T extends View> T getView(int id) {
        return (T)findViewById(id);
    }


    // ========================================================
    // add by liuxu, 2015-01-12, about action bar


    private void initActionBar(View contentView, ViewGroup.LayoutParams params) {
        mContentView = contentView;
        FrameLayout frame = getView(R.id.activity_base_content_frame);
        frame.removeAllViews();
        if (params == null) {
            frame.addView(contentView);
        } else {
            frame.addView(contentView, params);
        }
        ImageView titleBack = getView(R.id.activity_base_action_bar_back);
        ImageView titleMenu = getView(R.id.activity_base_action_bar_menu);
        TextView title = getView(R.id.activity_base_action_bar_title);
        // use activity label as default action bar title
        title.setText(getTitle());

        if (this instanceof ActionBarCallbacks) {
            // by implementing IvActionBarCallbacks, we assume a visible menu button is needed
            titleMenu.setVisibility(View.VISIBLE);
            final ActionBarCallbacks callbacks = (ActionBarCallbacks) this;
            titleBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!callbacks.onActionBarBackClicked(v)) {
                        BaseActivity.this.finish();
                    }
                }
            });
            titleMenu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callbacks.onActionBarMenuClicked(v);
                }
            });
            callbacks.onCreateActionBar(titleBack, titleMenu, title);

        } else {
            titleBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseActivity.this.finish();
                }
            });
        }
    }

    /**
     * by implementing this interface, activity extends from BaseActivity
     * will get an ActionBar with a back button and a title uses activity
     * label as text.
     * if menu button is needed, or further customize is needed, try implement
     * ActionBarCallbacks instead.
     */
    public interface ActionBarImpl {
    }

    /**
     * by implementing this interface, activity extends from BaseActivity
     * will get an ActionBar with a back button, a title uses activity
     * label as text, and a menu button.
     * if only back button and title is needed, try implement ActionBarImpl
     * instead.
     */
    public interface ActionBarCallbacks extends ActionBarImpl {

        /**
         * called when the ActionBar is first inflated.
         * @param back back button
         * @param menu menu button
         * @param title TextView for title
         */
        public void onCreateActionBar(ImageView back, ImageView menu, TextView title);

        /**
         * called when back button is clicked.
         * Activity.finish() will be called if false is returned from this method
         * @param back back button
         * @return true if click event is handled, false otherwise
         */
        public boolean onActionBarBackClicked(View back);

        /**
         * called when menu button is clicked.
         * @param menu menu button
         * @return true if click event is handled, false otherwise
         */
        public boolean onActionBarMenuClicked(View menu);
    }


    // ========================================================

}
